package com.avcdata.aoscustommade.utils.http;

import android.content.Context;
import android.provider.Settings;
import android.util.Log;

import com.avcdata.aoscustommade.R;
import com.avcdata.aoscustommade.utils.AvcLogUtils;
import com.avcdata.aoscustommade.utils.AvcNetworkUtil;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.SharedPreferencesUtils;
import com.avcdata.aoscustommade.utils.TextUitl;
import com.avcdata.aoscustommade.utils.ToastUtils;
import com.avcdata.national.network.BaseLog;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.BinaryHttpResponseHandler;

import org.apache.http.entity.ByteArrayEntity;
import org.json.JSONObject;

/**
 * Http请求工具类
 * Created by zyh on 2017/5/19.
 */

public class AvcHttpUtils {
    private static Context context = null;


    public AvcHttpUtils() {
        /*不能实例化异常*/
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static void init(Context context){
        AvcHttpUtils.context = context.getApplicationContext();
        BaseLog.e("动态设置URL","设置");
//        AvcConstants.URLHeaderBase = context.getResources().getString(R.string.URLHeaderBase);
//        AvcConstants.URLHeaderUserinfo = context.getResources().getString(R.string.URLHeaderUserinfo);
        BaseLog.e("动态设置URL","已设置：URLHeaderBase="+AvcConstants.URLHeaderBase+"<>URLHeaderUserinfo="+AvcConstants.URLHeaderUserinfo);
    }

    /**
     * post网路请求
     * @param url 请求地址
     * @param responseHandler 回调类
     */
    public static void post(String url, AvcResponseHandler responseHandler){
        JSONObject jsonObject = new JSONObject();
        AvcHttpUtils.post(url, jsonObject, false, responseHandler);
    }

    /**
     * post网路请求
     * @param url 请求地址
     * @param responseHandler 回调类
     */
    public static void post(String url, JSONObject jsonObject, AvcResponseHandler responseHandler){
        AvcHttpUtils.post(url, jsonObject, false, responseHandler);
    }

    /**
     * post 网络请求
     * @param url 请求地址
     * @param jsonObject 请求参数
     * @param responseHandler 回调类
     */
    public static void post(String url, JSONObject jsonObject, boolean isSessonId, AvcResponseHandler responseHandler) {
        Log.e("获取最新周",""+url);
        if (null == context){
            throw new RuntimeException("Not initialized");
        }
        if (!AvcNetworkUtil.isNetworkAvailable(context)){
            try {
                ToastUtils.showShort(context, "请连接网络");
                AvcJsonHttpResponseHandler handler = (AvcJsonHttpResponseHandler) responseHandler;
                handler.onFailure();
            } catch (Throwable e){
                AvcLogUtils.d("AvcHttpUtils", "网络请求回调请使用AvcJsonHttpResponseHandler类");
            }
            return;
        }
        responseHandler.setUrl(url);
        responseHandler.setJsonObject(jsonObject.toString());
        responseHandler.setBeginTime(System.currentTimeMillis());
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(AvcConstants.CONNECTTIME);
        ByteArrayEntity entity = null;
        try {
            entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
            if (isSessonId) {
                SharedPreferencesUtils sp = new SharedPreferencesUtils(context);
                if (TextUitl.isEmpty(sp.getSessionId())) {
                    ToastUtils.showShort(context, "请连接网络");
                    return;
                } else {
                    client.addHeader("sessionId", sp.getSessionId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        client.post(context, url, entity, "application/json", responseHandler);
    }

    /**
     * 下载数据使用，会返回byte数组
     * @param url
     * @param responseHandler
     */
    public static void get(String url, AvcResponseHandler responseHandler){
        if (null == context) {
            throw new RuntimeException("Not initialized");
        }
        if (!AvcNetworkUtil.isNetworkAvailable(context)) {
            ToastUtils.showShort(context, "请连接网络");
            return;
        }
        responseHandler.setUrl(url);
        responseHandler.setBeginTime(System.currentTimeMillis());
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(url, responseHandler);
    }
}
